<?php

namespace app\admin\controller;

use app\common\model\Member;

class ActivityMembers extends Home
{
    protected static $membersModel;

    public function __construct()
    {
        parent::__construct();
        self::$membersModel = new \app\common\model\ActivityMembers();
    }

    /**
     * 活动相关的用户列表
     * @return mixed
     */
    public function lists()
    {
        $activity_id = input('id');
        $list = self::$membersModel->where('activity_id', $activity_id)->order('id desc')->select();
        if ($list) {
            foreach ($list as $val) {
                $val->usernumber = Member::where('uid', $val['uid'])->value('usernumber');
                $addressInfo = Db('transport')->where('uid', $val['uid'])->where('status', 1)->find();
                if ($addressInfo) {
                    $area = implode("", $this->getAreaAddressThree($addressInfo['area']));
                    $addressInfo['address'] = $area . $addressInfo['address'];
                }
                $val->addressInfo = $addressInfo;
            }
        }

        $this->assign('_list', $list);
        $this->assign('title', '会员列表');
        return $this->fetch();
    }

    /**
     * 发放奖励
     * @return mixed
     */
    public function bonus()
    {
        $data = request()->post();
        $id = $data['id'];

        $minfo = self::$membersModel->get($id);
        if (!$minfo) {
            $this->apiError('参数错误');
        }

        if ($minfo->status == 1) {
            $this->apiError('该奖励已发放');
        }

        $res = self::$membersModel->where('id', $id)->update(['status' => 1]);

        if ($res) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }
}